<?php
namespace app\cms\controller;
use framework\ext\Check;
/* *
 * 资源特性与专题功能
 * */
class SpecialController extends \app\base\controller\AdminController{

    //分类首页
    public function index(){
        $act = form('get.act');
        if($act == 'del'){
            $fid = form('get.id',0,intval);
            $result =  model('special')->_delete($fid);
            if($result){
                $this->jsonMsg('删除成功',1);
            }else{
                $this->jsonMsg('删除失败!请查看是否包含子类');
            }
        }else{
            $page = form('get.page',0,intval);
            $this->pid = form('get.id',0,intval);  //当前ID
            $this->path = model('special')->path($this->pid,'special/index');
            $this->list = model('special')->pager($page,20)->_select($this->pid,$this->website_id);
            $this->pager = getPage(model('special')->pager,array('id'=>$this->pid));
            $this->display();
        }
    }
    //编辑分类
    public function edit(){
        if($this->isPost()){
            $msg = Check::rule(array(check::must($_POST['name']),'特性名称必须填写'));
            if(true !== $msg){
                $this->jsonMsg($msg);
            }
            //插入模型数据
            $data = array();
            $data = form('post',array('fid'));
            $data['uptime'] = time();
            if($data['pid'] == 0){
                $data['website_id'] = $this->website_id?$this->website_id:form('post.website_id',0,intval);
            }else{
                $root = model('special')->_find(array('id'=>$data['pid']));
                $data['website_id'] = $root['website_id'];
                $data['channel_id'] = $root['channel_id'];
            }
            $fid = form('post.fid',0,intval);
            if($fid){
                $condition['id'] = $fid;
                $rel = model('special')->_update($data,$condition);
            }else{
                $rel = model('special')->_update($data);
            }
            if($rel){
                $this->jsonMsg('资源特性添加成功',1,url('special/index',array('id'=>$_POST['pid'])));
            }else{
                $this->jsonMsg('资源特性添加失败');
            }
        }else{
            $this->fid = form('get.id',0,intval);
            $this->info = model('special')->_find(array('id'=>$this->fid));
            if(!$this->fid){
                $this->pid = form('get.pid',0,intval);
                if($this->pid){
                    $this->list = model('special')->_find(array('id'=>$this->pid),'cid');
                }else{
                    if($this->website_id){
                        $this->list = model('channel')->_select_list(array('0'=>'pid = 0 and type <= 1 and website_id = '.$this->website_id),'id desc','id,name');
                    }
                }
            }else{
                $this->pid = $this->info['pid'];
            }
            $this->path = model('special')->path($this->pid,'special/index');
            $this->display(); 
        }
    }

    //联动
    public function liandong(){
        if($this->isPost()){
           $id = form('post.current',0,intval);
           $data[0]['id'] = '0';
           $data[0]['name'] = '全站特性';
            if($id){
                $rel = model('channel')->_select_list(array('pid'=>0,'website_id'=>$id),'id desc','id,name');
                if($rel){
                    foreach ($rel as $key => $value) {
                       $data[$key+1]['id'] = $value['id'];
                       $data[$key+1]['name'] = $value['name'];
                    }
                }
            }
            echo json_encode($data);
        }
    }
}